﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    public class WxHelp
    {
        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="parameterStr"></param>
        /// <returns></returns>
        public static string GetSHA1(string parameterStr)
        {
            var output = string.Empty;
            var sha1 = new SHA1CryptoServiceProvider();
            var inputBytes = UTF8Encoding.UTF8.GetBytes(parameterStr);
            var outputBytes = sha1.ComputeHash(inputBytes);
            sha1.Clear();
            output = BitConverter.ToString(outputBytes);
            return output;
        }


        ///// <summary>
        ///// 返回参数
        ///// </summary>
        ///// <param name="message"></param>
        ///// <returns></returns>
        //public static HttpResponseMessage GetReturn(string message)
        //{
        //    return new HttpResponseMessage
        //    {
        //        Content = new StringContent(message, Encoding.UTF8, "application/json")
        //    };
        //}





        /// <summary>
        /// 发送被动响应消息
        /// </summary>
        /// <param name="ToUserName">接收方帐号（收到的OpenID）</param>
        /// <param name="Content">回复的消息内容（换行：在content中能够换行，微信客户端就支持换行显示）</param>
        /// <param name="FromUserName">开发者微信号</param>
        /// <param name="CreateTime">消息创建时间 （整型）</param>
        /// <param name="MsgType">text</param>
        /// <returns></returns>
        public static string GetSendMessage(string ToUserName, string Content, string FromUserName = "Developer",
                                     string MsgType = "text")
        {
            var createTime = DateTime.Now.Ticks;
            return string.Format(@"<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime><MsgType><![CDATA[{3}]]></MsgType><Content><![CDATA[{4}]]></Content></xml>", ToUserName, FromUserName, createTime, MsgType, Content);
        }

        /// <summary>
        /// 获取消息体中正则所能匹配到的内容
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="regex">正则</param>
        /// <returns>返回正则匹配的所有内容</returns>
        public static string GetItemValue(string message, Regex regex)
        {
            if (regex.IsMatch(message))
                return regex.Match(message).Value;
            return "";
        }

        /// <summary>
        /// 发送图片信息
        /// </summary>
        /// <param name="toUserName">接收方帐号（收到的OpenID）</param>
        /// <param name="fromUserName">开发者微信号</param>
        /// <param name="mediaId">素材ID</param>
        /// <returns></returns>
        public static string GetSendImgMessage(string toUserName, string fromUserName, string mediaId)
        {
            var createTime = DateTime.Now.Ticks;
            return string.Format(@"<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[{3}]]></MediaId></Image></xml>",
                toUserName, fromUserName, createTime, mediaId);
        }

    }
}
